#!/bin/bash
# Convenience script to create a stand-alone bundle and a 
# disk image to distribute Perceel to Mac OS X users.
# Thanks to PasNox

QT_PATH_FRAMEWORKS=/Library/Frameworks
PERCEEL_APP=./Perceel.app
PERCEEL_FRAMEWORKS=$PERCEEL_APP/Contents/Frameworks
PERCEEL=$PERCEEL_APP/Contents/MacOS/Perceel
 
echo "Copying Qt 4 frameworks..."
mkdir -p $PERCEEL_FRAMEWORKS
cp $QT_PATH_FRAMEWORKS/QtCore.framework/Versions/4/QtCore $PERCEEL_FRAMEWORKS
cp $QT_PATH_FRAMEWORKS/QtGui.framework/Versions/4/QtGui $PERCEEL_FRAMEWORKS
cp $QT_PATH_FRAMEWORKS/QtXml.framework/Versions/4/QtXml $PERCEEL_FRAMEWORKS

echo "Copying resources..."
mkdir -p $PERCEEL_APP/Contents/Resources
#cp help.pdf $PERCEEL_APP/Contents/Resources
#cp license.pdf $PERCEEL_APP/Contents/Resources
 
echo "Relinking frameworks..."
install_name_tool -id @executable_path/../Frameworks/QtCore $PERCEEL_FRAMEWORKS/QtCore
install_name_tool -id @executable_path/../Frameworks/QtGui $PERCEEL_FRAMEWORKS/QtGui
install_name_tool -id @executable_path/../Frameworks/QtXml $PERCEEL_FRAMEWORKS/QtXml
 
install_name_tool -change QtCore.framework/Versions/4/QtCore @executable_path/../Frameworks/QtCore $PERCEEL_FRAMEWORKS/QtGui
install_name_tool -change QtCore.framework/Versions/4/QtCore @executable_path/../Frameworks/QtCore $PERCEEL_FRAMEWORKS/QtXml
 
install_name_tool -change QtCore.framework/Versions/4/QtCore @executable_path/../Frameworks/QtCore $PERCEEL
install_name_tool -change QtGui.framework/Versions/4/QtGui @executable_path/../Frameworks/QtGui $PERCEEL
install_name_tool -change QtXml.framework/Versions/4/QtXml @executable_path/../Frameworks/QtXml $PERCEEL
 
echo "Creating temporary dmg..."
hdiutil create -megabytes 30 tmp.dmg -layout NONE -fs HFS+ -volname Perceel -ov
tmp=`hdid tmp.dmg`
disk=`echo $tmp | cut -f 1 -d\ `
 
echo "Copying Perceel..."
cp -R $PERCEEL_APP /Volumes/Perceel/
 
echo "Copying standard files..."
cp help.pdf /Volumes/Perceel/Help.pdf
cp license.pdf /Volumes/Perceel/License.pdf

#cp ../../../ChangeLog /Volumes/Perceel/
#cp ../../../README.QT /Volumes/Perceel/
#cp ../../../AUTHORS /Volumes/Perceel/
#cp ../../../COPYING /Volumes/Perceel/
 
echo "Creating release dmg..."
hdiutil eject $disk
rm -f Perceel.dmg
hdiutil convert -format UDZO tmp.dmg -o Perceel.dmg
rm tmp.dmg
